#include "AppStateMenu.h"
#include "AppStateManager.h"
#include "Random.h"

void CAppStateMenu::OnKeyMainMenuChoice(SDLKey sym)
{
	switch(sym) {

		//Up one option
		case SDLK_UP:
		{
			CSoundBank::SoundControl.Play(0, 1);
			choice--;
			if(choice<0)
				choice = 5;
				
			break;
		}
		//down one option
		case SDLK_DOWN:
		{
			CSoundBank::SoundControl.Play(0, 1);
			this->choice++;
			if(this->choice>5)
				this->choice = 0;
				
			break;
		}

		//down one option
		case SDLK_s:
		{
			CSoundBank::SoundControl.Play(0, 1);
			this->choice++;
			if(this->choice>5)
				this->choice = 0;
				
			break;
		}

		//Up one option
		case SDLK_w:
		{
			CSoundBank::SoundControl.Play(0, 1);
			choice--;
			if(choice<0)
				choice = 5;
				
			break;
		}
		//GoBack/Exit button
		case SDLK_ESCAPE:
		{
			OnDeactivate();
			CAppStateManager::SetActiveAppState(APPSTATE_NONE);
			SDL_Quit();
			break;
		}
		
		//The confirm button
		case SDLK_RETURN:
		{
			switch(choice)
			{
				case 0:
				{
					//Selected menu item "Join Game"
					mainChoice = JOIN_CHOICE;
					writeIP = true;
					break;
				}
				case 1:
				{
					CSoundBank::SoundControl.Stop();
					OnDeactivate();
					CAppStateManager::SetActiveAppState(APPSTATE_SERVER);
				}
				case 2:
				{
					//Selected menu item "Customize"
					mainChoice = NAME_CHOICE;
					for(int i = 0; i < 5; i++)
					{
						items[i] = 0;
					}
					break;
				}
				case 3:
				{
					//Selected menu item "Options"
					mainChoice = OPTIONS_CHOICE;
					choice = 0;
					
					break;
				}
				case 4:
				{
					// Selected menu item "Credits".
					CAppStateManager::SetActiveAppState(APPSTATE_CREDITS);
					break;
				}
				case 5:
				{
					
					// Selected menu item "Exit".
					OnDeactivate();
					CAppStateManager::SetActiveAppState(APPSTATE_NONE);
					SDL_Quit();
					break;
				}
				default:
				{

				}
			}
		}
		default: 
		{
		}
	}
}

void CAppStateMenu::OnLoopAnimation()
{
		//Animations for menu (fireball, cloud etc)
	sky1.x -= 0.03f;
	sky2.x -= 0.02f;
	sky3.x -= 0.01f;
	fire1.x += 5;
	fire2.x += 6;
	if(fire1.x > 650)
	{
		fire1.x = 0;
		fire1.y = Random(0, 280);
	}
	if(fire2.x > 650)
	{
		fire2.x = 0;
		fire2.y = Random(0, 280);
	}
	if(sky1.x <= 0)
	{
		sky1.x = 680;
		sky1.y = Random(0, 280);
	}
	if(sky2.x <= 0)
	{
		sky2.x = 680;
		sky2.y = Random(0, 280);
	}
	if(sky3.x <= 0)
	{
		sky3.x = 680;
		sky3.y = Random(0, 280);
	}

	if (!CSoundBank::SoundControl.IsPlaying())
	{
		CSoundBank::SoundControl.Play(0,0);
	}

	SDL_Delay(10);
}

void CAppStateMenu::OnRenderMainWindow(SDL_Surface* Surf_Display)
{
	//current choice dot.
	SDL_Rect MenuRect;
	MenuRect.w = 10;
	MenuRect.h = 10;

	SDL_Surface* ChoicesTextSurface;

	//draw background
	SDL_BlitSurface(menuBG, NULL, Surf_Display, NULL);
	
	//Draw skies and fireballs
	SDL_BlitSurface(sky, NULL, Surf_Display, &sky1);
	SDL_BlitSurface(sky, NULL, Surf_Display, &sky2);
	SDL_BlitSurface(sky, NULL, Surf_Display, &sky3);
	SDL_BlitSurface(spell, NULL, Surf_Display, &fire1);
	SDL_BlitSurface(spell, NULL, Surf_Display, &fire2);

	//Setting position of the cursor on main menu
	MenuRect.x = 180;
	MenuRect.y = 150 + 40*choice;
	SDL_FillRect(Surf_Display, &MenuRect, 0);

	//Header Text
	SDL_Rect TextPos;
	TextPos.x = (WWIDTH/2) - 150;
	TextPos.y = 10;
	SDL_Colour black = {0,0,0};
	std::string header = "Da'Mage";
	ChoicesTextSurface = TTF_RenderText_Blended(mainTextfont, header.c_str(), black);
	SDL_BlitSurface(ChoicesTextSurface, NULL, Surf_Display, &TextPos);
	SDL_FreeSurface(ChoicesTextSurface);
	
	//Render  game options text
	std::string nameOfChoices[6] = {"Join Game", "Host Game", "Customize", "Options", "Credit", "Exit"};
	TextPos.x = (WWIDTH/2) - 120;

	for(int i = 0; i < 6; i++)
	{
		TextPos.y = 140 + 40*i;
		ChoicesTextSurface = TTF_RenderText_Blended(menuChoiceFont, nameOfChoices[i].c_str(), black);
		SDL_BlitSurface(ChoicesTextSurface, NULL, Surf_Display, &TextPos);
		SDL_FreeSurface(ChoicesTextSurface);
	}
}